package controller;

import config.JwtConfig;
import entity.Result;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import service.impl.UserServiceImpl;
import javax.annotation.Resource;

@RestController
@CrossOrigin
public class UserController {
    @Resource
    UserServiceImpl userService;

    @Autowired
    JwtConfig jwtConfig;

   @RequestMapping("/selectById")
    public User selectById(Integer id){
        return userService.selectById(id);
    }

    @RequestMapping("/addUser")
    public int addUser(@RequestBody User user){
       return userService.addUser(user);
    }
    /*
    jwt:用于用户的登陆验证,验证用户登陆状态的有效性
    1.用户发送请求,访问后端的controller前,验证用户登录有效时间
    2.需要配置拦截器,在拦截器中进行登陆验证
    3.根据验证的结果响应用户不同的数据
    */

    @RequestMapping("/login")
     public Result login(String username, String password){
        System.out.println(username+","+password);
        //用户登录操作,应当生成token
        //拦截器不应该拦截login请求,拦截非login进行token验证
        String token= jwtConfig.createToken(username);
        System.out.println(token);
         return Result.success(token);
     }


}
